from gi.repository import Gtk


class CustomFormDialog(Gtk.Window):
    def __init__(self, parent, title: str, fields: list[str], on_submit_clicked, **kwargs):
        self.on_submit_clicked = on_submit_clicked
        super().__init__(**kwargs)
        self.set_transient_for(parent)
        self.set_modal(True)
        self.set_title(title)

        # 创建表单内容
        content_area = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
        self.set_child(content_area)

        # 创建输入字段
        entries = []
        for field in fields:
            field_label = Gtk.Label(label=field + ":", halign=Gtk.Align.START)
            field_entry = Gtk.Entry()
            content_area.append(field_label)
            content_area.append(field_entry)
            entries.append((field, field_entry))

        # 创建按钮
        button_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=6)
        content_area.append(button_box)

        cancel_button = Gtk.Button(label="取消")
        cancel_button.connect("clicked", lambda btn: self.destroy())
        button_box.append(cancel_button)

        submit_button = Gtk.Button(label="确定")
        submit_button.connect("clicked", self.on_submit_clicked, *entries)
        button_box.append(submit_button)
